package com.dustedpixels.jasmin.chips.z80.util;

import java.util.Random;

import com.dustedpixels.jasmin.chips.z80.Z80;

/**
 * Generates random state for Z80 chip (random register values).
 */
public final class Z80RandomStateGenerator {
    private final Random random = new Random();

    /**
     * Generates random state for specified Z80 chip based on seed number. It's
     * guaranteed, that invoking this method twice for the same seed generates
     * identical states.
     * 
     * @param z80 The Z80 chip
     * @param seed The seed value
     */
    void generateRandomState(Z80 z80, long seed) {
        random.setSeed(seed);

        z80.setAF((short) (random.nextInt() & 0xFFFF));
        z80.setBC((short) (random.nextInt() & 0xFFFF));
        z80.setDE((short) (random.nextInt() & 0xFFFF));
        z80.setHL((short) (random.nextInt() & 0xFFFF));
        z80.setAF2((short) (random.nextInt() & 0xFFFF));
        z80.setBC2((short) (random.nextInt() & 0xFFFF));
        z80.setDE2((short) (random.nextInt() & 0xFFFF));
        z80.setHL2((short) (random.nextInt() & 0xFFFF));
        z80.setIX((short) (random.nextInt() & 0xFFFF));
        z80.setIY((short) (random.nextInt() & 0xFFFF));
        z80.setSP((short) (random.nextInt() & 0xFFFF));
        z80.setPC((short) (random.nextInt() & 0xFFFF));
        z80.setI((byte) (random.nextInt() & 0xFF));
        z80.setR((byte) (random.nextInt() & 0xFF));
        z80.setIFF1(random.nextBoolean());
        z80.setIFF2(random.nextBoolean());
        z80.setIM((byte) (Math.abs(random.nextInt()) % 3));
    }
}
